package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Troublerecord;

import com.woniuxy.caradmin.entity.vo.TroublerecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.TroublerecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/troublerecord")
public class TroublerecordController {
@Autowired
    private TroublerecordService troublerecordService;
    @GetMapping("/troublerecordById")
    @ApiOperation(value = "id查询年检记录")
    public R troublerecordById(Integer id) {

        Troublerecord troublerecord = troublerecordService.selecTroubleById(id);
        if (troublerecord !=null) {

            return R.ok().data("troublerecord",troublerecord);
        } else {
            throw new CarException(ResultCode.TROUBLEISNULL, Constaint.TROUBLEISNULL);
        }
    }

@GetMapping("/troublerecordList")
@ApiOperation(value = "查询事故记录")
    public DataGridView troublerecordList(TroublerecordVo troublerecordVo){
    Page<Object> page=PageHelper.startPage(troublerecordVo.getPage(),troublerecordVo.getLimit());
    List<Troublerecord> troublerecords = troublerecordService.selectAllByPage(troublerecordVo);
    if(troublerecords!=null&&troublerecords.size()>0 ){
        return new DataGridView(page.getTotal(),troublerecords);
    }else {
        throw new CarException(ResultCode.TROUBLEISNULL, Constaint.TROUBLEISNULL);
    }
}
    @GetMapping("/addtroublerecord")
    @ApiOperation(value = "提交事故记录")
    public R addtroublerecord(Troublerecord troublerecord, HttpServletRequest httpServletRequest){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String vid = httpServletRequest.getParameter("vid");
        String driverid = httpServletRequest.getParameter("driverid");
        String address = httpServletRequest.getParameter("address");
        String time = httpServletRequest.getParameter("time");
        String reason = httpServletRequest.getParameter("reason");
        String oursitu = httpServletRequest.getParameter("oursitu");
        String othersitu = httpServletRequest.getParameter("othersitu");
        String result = httpServletRequest.getParameter("result");
        String ourmoney = httpServletRequest.getParameter("ourmoney");
        String othermoney = httpServletRequest.getParameter("othermoney");
        String insuremoney = httpServletRequest.getParameter("insuremoney");
        String information = httpServletRequest.getParameter("information");
        troublerecord.setVid(Integer.parseInt(vid));
        troublerecord.setDriverid(Integer.parseInt(driverid));
        troublerecord.setAddress(address);
        troublerecord.setTime(time);
        troublerecord.setReason(reason);
        troublerecord.setOursitu(oursitu);
        troublerecord.setOthersitu(othersitu);
        troublerecord.setOurmoney(Double.parseDouble(ourmoney));
        troublerecord.setOthermoney(Double.parseDouble(othermoney));
        troublerecord.setResult(result);
        troublerecord.setInsuremoney(Double.parseDouble(insuremoney));
        troublerecord.setInformation(information);
        troublerecord.setStatus(1);
        Date parse=null;
        try {
             parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date = new Date();
        if(date.getTime()<parse.getTime()){
            throw  new CarException(ResultCode.TIMESHEZHI,Constaint.TIMESHEZHI);
        }
        int i = troublerecordService.addTroublerecord(troublerecord);
        if(i>0 ){
            return R.ok().code(ResultCode.ADDTRUBLESUC).message(Constaint.ADDTRUBLESUC);
        }else {
            throw new CarException(ResultCode.ADDTROUBLEFAIL, Constaint.ADDTROUBLEFAIL);
        }
    }
    @GetMapping("/updateroublerecord")
    @ApiOperation(value = "修正事故记录")
    public R updateroublerecord(Troublerecord troublerecord){

        int i = troublerecordService.updateTroublerecord(troublerecord);
        if(i>0 ){
            return R.ok().code(ResultCode.UPTROUBLESUC).message(Constaint.UPTROUBLESUC);
        }else {
            throw new CarException(ResultCode.UPTROUBLEFAIL, Constaint.UPTROUBLEFAIL);
        }
    }
    @GetMapping("/removeroublerecord")
    @ApiOperation(value = "删除事故记录")
    public R removeroublerecord(Integer []ids){

        boolean b = troublerecordService.removeT(ids);
        if(b ){
            return R.ok().code(ResultCode.REMOVETRUBLESUC).message(Constaint.REMOVETRUBLESUC);
        }else {
            throw new CarException(ResultCode.REMOVETRUBLESFAIL, Constaint.REMOVETRUBLESFAIL);
        }
    }
}

